package com.bourse.core.ibatisHandler;

import java.sql.SQLException;
import java.sql.Types;

import com.bourse.core.services.utils.IsinCodeFactory;
import com.bourse.core.utils.IsinCode;
import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

public class IsinCodeTypeHandler implements TypeHandlerCallback {

	public void setParameter(ParameterSetter setter, Object obj)
			throws SQLException {
		if (obj == null) {
			setter.setNull(Types.LONGVARCHAR);
		} else if (obj instanceof IsinCode) {
			IsinCode isinCode = (IsinCode)obj;
			setter.setString(isinCode.getCode());
		} else {
			throw new IllegalArgumentException("Unexpected object type : " + obj.getClass());
		}		
	}

	public Object getResult(ResultGetter getter) throws SQLException {
		String isin = getter.getString();
		if (getter.wasNull()) {
			return null;
		}
		return IsinCodeFactory.getInstance().getIsinCode(isin);
	}

	public Object valueOf(String s) {
		if(s==null){
			return null;
		}
		IsinCode isin = IsinCodeFactory.getInstance().getIsinCode(s);
		return isin;
	}

}
